package com.tx.utils.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: tcx
 * @description: 对博客操作的拦截器
 * @Date: 2019/11/10
 **/
public class BlogInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //获取请求路径
        String url = httpServletRequest.getRequestURI();
        //对博客添加操作进行拦截
        if(url.toLowerCase().indexOf("add")>0){
            HttpSession session = httpServletRequest.getSession();
            if (session.getAttribute("user")!=null){
                return true;
            }else{
                //重定向到登录页面
                httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/user/to_login.do");
                return false;
            }

        }else{
            return true;
        }
        //重定向到登录页面
       // httpServletResponse.sendRedirect("/to_login.do");
       // return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
